Skip to content

Fix cert renewal signing the PPPK challenge with the wrong private key - #66

Merged
DonaldChung-HK merged 1 commit into
mainfrom
Fix-PPPK-Failure
Oct 13, 2026
Merged

DonaldChung-HK merged 1 commit into
mainfrom
Fix-PPPK-Failure

Conversation

@DonaldChung-HK

@DonaldChung-HK DonaldChung-HK commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes online certificate renewal failing with 403 when the signing key is picked out of a keystore that holds the same certificate under more than one alias.

  • ClientKeyStore.getPrivateKey(PublicKey) now uses the alias that was matched when scanning for the requested public key, instead of re-deriving it.
  • MainWindowPanel pins the "Valid from / to" display formatter to Locale.UK.
    • User preference issue

Problem

getPrivateKey(PublicKey) walks the keystore aliases, finds the key entry whose certificate matches the requested public key, then discards the alias it just matched and re-derives it:

String _alias = this.keyStore.getCertificateAlias(cert);
return (PrivateKey) this.keyStore.getKey(_alias, PASSPHRASE);

KeyStore.getCertificateAlias returns the first alias whose entry holds that certificate. If the certificate appears under more than one alias, that need not be the entry that was matched, so the returned private key can belong to a different alias. If the first match happens to be a trustedCertEntry, it has no key at all and the call throws UnrecoverableKeyException, which the catch block swallows into a null return. Either way the renewal request signed the PPPK challenge with the wrong key and the CA rejected it with 403.

Original bug introduced in Commit: d362b3f (parent db5c9af)
File: src/uk/ngs/ca/common/ClientKeyStore.java

Before — d362b3f^ (2011-06-16), lines 148-172:

    public synchronized PrivateKey getPrivateKey(PublicKey publicKey) {
        PrivateKey privateKey = null;
        try {
            Enumeration aliases = this.keyStore.aliases();
            List vector = new ArrayList(0);
            while (aliases.hasMoreElements()) {
                String alias = (String) aliases.nextElement();
                if (this.keyStore.isKeyEntry(alias)) {
                    vector.add(this.keyStore.getCertificate(alias));
                }
            }
            for (int i = 0; i < vector.size(); i++) {
                X509Certificate cert = (X509Certificate) vector.get(i);
                PublicKey _publicKey = cert.getPublicKey();
                if (publicKey.equals(_publicKey)) {
                    String _alias = this.keyStore.getCertificateAlias(cert);
                    privateKey = (PrivateKey) this.keyStore.getKey(_alias, PASSPHRASE);
                }
            }
        } catch (KeyStoreException ke) {
            ke.printStackTrace();
        } finally {
            return privateKey;
        }
    }

After — d362b3f, lines 147-164:

    public synchronized PrivateKey getPrivateKey(PublicKey publicKey) {
        try {
            Enumeration aliases = this.keyStore.aliases();
            while (aliases.hasMoreElements()) {
                String alias = (String) aliases.nextElement();
                if (this.keyStore.isKeyEntry(alias)) {
                    X509Certificate cert = (X509Certificate) this.keyStore.getCertificate(alias);
                    if(cert.getPublicKey().equals(publicKey)){
                        String _alias = this.keyStore.getCertificateAlias(cert);
                        return (PrivateKey) this.keyStore.getKey(_alias, PASSPHRASE);
                    }
                }
            }     
        } catch (Exception ke) {
            ke.printStackTrace();
        }
        return null;
    }

Fix

  • Pass the matched alias directly to getKey(alias, PASSPHRASE), so certificate and private key always come from the same keystore entry. getCertificateAlias is no longer used here.
  • Pin the Valid from/to formatter to Locale.UK so the panel renders identically whatever the host's default locale is (e.g. th-TH Buddhist calendar, ar-SA). This string is display only - never parsed, stored or sent - so it does not affect the PPPK challenge on the wire.

Test plan

  • mvn test (CI: temurin JDK 25) - no existing unit test covers ClientKeyStore; the changed branch is a single alias lookup.
  • Manual: asking user to renew problematic certificate. Done, issue not exist anymore.

- ClientKeyStore.getPrivateKey(PublicKey) located the matching key entry by alias, then discarded that alias and re-derived it via getCertificateAlias(), which returns the first alias holding the certificate. Where a keystore has the same certificate under more than one alias, this paired the requested certificate with another entry's private key, so the CA could not verify the PPPK response and rejected the renewal with 403.
- Pin the Valid from/to display formatter to Locale.UK: British rendering is intended, since we are based in the UK, user would prefer british "Sept"

@garaimanoj garaimanoj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM.
I would appreciate it if comments could be avoided where possible. If required, a short single-line comment would be sufficient.

@DonaldChung-HK
DonaldChung-HK marked this pull request as ready for review October 13, 2026 08:51
@DonaldChung-HK
DonaldChung-HK merged commit 79fc561 into main Oct 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants